package com.gouaixin.bee.beeframework2.tool;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by Administrator on 2015/3/20.
 */
public class XHttpUtils {

    public static String post(String url, String json) {
        final String[] result = {""};

        RequestParams params = new RequestParams();



        params.addBodyParameter("json", json);

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                url,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        mLog.e(current + "/" + total);
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mLog.e("响应=" + XHttpUtils.Unicode2Chinese(responseInfo.result));
                        result[0] = XHttpUtils.Unicode2Chinese(responseInfo.result);
                        mMyLister.myMethod(result[0]);
                    }

                    @Override
                    public void onStart() {
                        mLog.e("开始：");
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mLog.e("故障：" + msg);
                    }
                }
        );
        return result[0];


    }

    //自定义一个回调方法
    /**
     * 定义一个接口
     */
    public interface MyLister{
        public void myMethod(String str);
    }

    /**
     * 初始化接口变量
     */
    private static MyLister mMyLister = null;

    /**
     * 自定义控件的自定义事件
     */
    public static void setMyLister(MyLister mMyLister2)
    {
        mMyLister = mMyLister2;
    }


    /**
     * Unicode转中文
     *
     * @param theString
     * @return
     */
    public static String Unicode2Chinese(String theString) {

        char aChar;

        int len = theString.length();

        StringBuffer outBuffer = new StringBuffer(len);

        for (int x = 0; x < len; ) {

            aChar = theString.charAt(x++);

            if (aChar == '\\') {

                aChar = theString.charAt(x++);

                if (aChar == 'u') {

                    // Read the xxxx

                    int value = 0;

                    for (int i = 0; i < 4; i++) {

                        aChar = theString.charAt(x++);

                        switch (aChar) {

                            case '0':

                            case '1':

                            case '2':

                            case '3':

                            case '4':

                            case '5':

                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';

                    else if (aChar == 'n')

                        aChar = '\n';

                    else if (aChar == 'f')

                        aChar = '\f';

                    outBuffer.append(aChar);

                }

            } else

                outBuffer.append(aChar);

        }

        return outBuffer.toString();

    }

    /**
     * *
     * 全部转Unicode
     *
     * @param gbString
     * @return
     */
    public static String All2Unicode(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
            String hexB = Integer.toHexString(utfBytes[byteIndex]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        System.out.println("unicodeBytes is: " + unicodeBytes);
        return unicodeBytes;
    }




    public static String getTuijian() {
        String url="";
        final String[] result = {""};

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        mLog.e(current + "/" + total);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mLog.e("请求成功：" + Unicode2Chinese(responseInfo.result));
                        result[0] =Unicode2Chinese(responseInfo.result);
                    }

                    @Override
                    public void onStart() {
                        mLog.e("开始请求：");
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mLog.e("请求故障：" + msg);
                    }
                }
        );
        return result[0];
    }

}


